//
//  ZYLFrontCollectionView.m
//  Cartoon
//
//  Created by dllo on 15/7/13.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ZYLFindFrontCollectionView.h"
#import "Define.h"
#import "ZYLFindFrontLayout.h"
#import "ZYLFindFrontCollectionViewCell.h"
#import "ZYLFindFrontSigleLayout.h"
#import "ZYLSigleCollectionViewCell.h"
#import "ZYLLittleCollectionViewCell.h"
@implementation ZYLFindFrontCollectionView

- (void)dealloc
{
    [_str release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    UICollectionViewLayout *layout = [[[ZYLFindFrontSigleLayout alloc] init] autorelease];
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        [self createCollection];
    }
    return self;
}

#pragma mark - 创建collectionView
- (void)createCollection
{
    self.scrollEnabled = NO;
    self.backgroundColor = [UIColor whiteColor];
    self.showsVerticalScrollIndicator = NO;
    self.backgroundColor = [UIColor whiteColor];
    [self registerClass:[ZYLLittleCollectionViewCell class] forCellWithReuseIdentifier:@"frontCollectionView"];
    [self registerClass:[ZYLFindFrontCollectionViewCell class] forCellWithReuseIdentifier:@"frontCollectionView111"];
    [self registerClass:[ZYLLittleCollectionViewCell class] forCellWithReuseIdentifier:@"frontCollectionView222"];
    [self registerClass:[ZYLSigleCollectionViewCell class] forCellWithReuseIdentifier:@"frontCollectionView333"];
}

#pragma mark - 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.model.Myaction substringToIndex:5] isEqualToString:@"topic"]) {
        NSInteger i = self.model.topics.count + 1;
        if (self.model.topics.count % 2 != 0) {
            i = self.model.topics.count;
        }
        if (indexPath.item == 0 || indexPath.item == i) {
            ZYLLittleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"frontCollectionView" forIndexPath:indexPath];
            if (indexPath.item != 0) {
                cell.title.text = @"全部专题";
            } else {
                cell.title.text = self.model.title;
            }
            return cell;
        } else {
            ZYLFindFrontCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"frontCollectionView111" forIndexPath:indexPath];
            cell.topicModel = self.model.topics[indexPath.row - 1];
            return cell;
        }
    }
    else {
        NSInteger i = self.model.comics.count + 1;
        if (self.model.comics.count % 2 != 0) {
            i = self.model.comics.count;
        }
        if (indexPath.item == 0 || indexPath.item == i) {
            ZYLLittleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"frontCollectionView222" forIndexPath:indexPath];
            if (indexPath.item != 0) {
                cell.title.text = @"全部专题";
            } else {
                cell.title.text = self.model.title;
            }
            return cell;
        } else {
            ZYLSigleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"frontCollectionView333" forIndexPath:indexPath];
            cell.comicModel = self.model.comics[indexPath.row - 1];
            return cell;
        }
    }
    return nil;
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.model.Myaction substringToIndex:5] isEqualToString:@"topic"]) {
        NSInteger i = self.model.topics.count + 1;
        if (self.model.topics.count % 2 != 0) {
            i = self.model.topics.count;
        }
        if (indexPath.item == 0 || indexPath.item == i) {
            if (indexPath.item != 0) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ZYLFindPushToAllTopic" object:nil];
            }
        } else {
            NSNumber *topicID = [NSNumber numberWithInteger:[[self.model.topics[indexPath.item - 1] searchid] integerValue]];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ZYLFindPushToTopic" object:topicID];
        }
    }
    else {
        NSInteger i = self.model.comics.count + 1;
        if (self.model.comics.count % 2 != 0) {
            i = self.model.comics.count;
        }
        if (indexPath.item == 0 || indexPath.item == i) {
            if (indexPath.item != 0) {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ZYLFindPushToAllTopic" object:nil];
            }
        } else {
            NSNumber *comicID = [NSNumber numberWithInteger:[[self.model.comics[indexPath.item - 1] comicsID] integerValue]];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ZYLFindPushToComic" object:comicID];
        }
    }
}

#pragma mark - section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

#pragma mark - item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if ([[self.model.Myaction substringToIndex:5] isEqualToString:@"topic"]) {
        if (self.model.topics.count % 2 != 0) {
            return self.model.topics.count + 1;
        }
        return self.model.topics.count + 2;
    } else {
        if (self.model.comics.count % 2 != 0) {
            return self.model.comics.count + 1;
        }
        return self.model.comics.count + 2;
    }
}
- (void)setModel:(ZYLTopic_listsMixedModel *)model
{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    [self reloadData];
}
@end
